from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QBrush, QFont, QIcon
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QPushButton
from modules import *

class TableWidget(QTableWidget):

    def __init__(self, type = 'all'):
        super(QTableWidget, self).__init__()
        if type == 'all':
            self.column = 5
        elif type == 'album':
            self.column = 4
        self.type = type
        self.songsList = []
        self.setupUi()

    def setupUi(self):
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.sizePolicy().hasHeightForWidth())
        self.setSizePolicy(sizePolicy)
        font = QtGui.QFont()
        font.setPointSize(10)
        font.setStyleStrategy(QtGui.QFont.PreferDefault)
        self.setFont(font)
        self.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
        self.setAutoScroll(True)
        self.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.setDragEnabled(False)
        self.setAlternatingRowColors(True)
        self.setSelectionMode(QtWidgets.QAbstractItemView.NoSelection)
        self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectItems)
        self.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerItem)
        self.setShowGrid(False)
        self.setGridStyle(QtCore.Qt.NoPen)
        self.setWordWrap(False)
        self.setCornerButtonEnabled(True)
        self.setRowCount(5)
        self.setColumnCount(self.column)
        self.setObjectName("tableWidget")
        self.horizontalHeader().setVisible(False)
        self.horizontalHeader().setCascadingSectionResizes(False)
        self.horizontalHeader().setDefaultSectionSize(200)
        self.horizontalHeader().setHighlightSections(True)
        self.verticalHeader().setVisible(False)
        self.verticalHeader().setCascadingSectionResizes(False)
        self.verticalHeader().setDefaultSectionSize(50)
        self.verticalHeader().setHighlightSections(True)

        self.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.Stretch)
        # self.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.ResizeToContents)
        self.horizontalHeader().setSectionResizeMode(0, QtWidgets.QHeaderView.Interactive)
        # self.horizontalHeader().setSectionResizeMode(2, QtWidgets.QHeaderView.Interactive)
        # self.horizontalHeader().setSectionResizeMode(3, QtWidgets.QHeaderView.Interactive)
        self.setColumnWidth(0, 40)
        # self.setColumnWidth(2, 150)
        # self.setColumnWidth(3, 200)
        self.setFocusPolicy(Qt.NoFocus)

        self.setMouseTracking(True)
        self.previousRow = -1
        self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
        self.cellEntered.connect(self.onCellEntered)
        self.itemDoubleClicked.connect(self.playSong)

        CommonSignal.changeSongSignal.connect(self.getPlayingSong)
        # CommonSignal.musicTabChangedSignal.connect(self.loadSongsList)

    def leaveEvent(self, e):
        if self.previousRow != -1:
            self.removeCellWidget(self.previousRow, 2)
            self.setRowColor(self.previousRow, self.lastRowColor)
            self.previousRow = -1

    def onCellEntered(self, row, column):

        if self.previousRow != -1:
            self.removeCellWidget(self.previousRow,2)
            self.setRowColor(self.previousRow, self.lastRowColor)

        self.lastRowColor = self.item(row, 0).background()
        self.setRowColor(row, QColor(217,217,217))
        self.previousRow = row


        # PlaySongButton = QtWidgets.QPushButton()
        # PlaySongButton.setMinimumSize(QtCore.QSize(50, 50))
        # PlaySongButton.setMaximumSize(QtCore.QSize(50, 50))
        # PlaySongButton.setText("")
        # icon = QtGui.QIcon()
        # icon.addPixmap(QtGui.QPixmap(":/icons/images/icon/play_5.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        # PlaySongButton.setIcon(icon)
        # PlaySongButton.setIconSize(QtCore.QSize(30, 30))
        # PlaySongButton.setObjectName("PlaySongButton")
        # songId = self.songsList[row][0]
        # PlaySongButton.clicked.connect(lambda: CommonSignal.playSongSignal.emit(songId))
        # self.setCellWidget(row, 2, PlaySongButton)

        # print(self.item(row, 1).text())

    def setRowColor(self, row:int, color:QColor):
        for col in range(0, self.columnCount()):
            item = self.item(row, col)
            item.setBackground(QBrush(color))

    def loadSongsList(self, songs):
        # row = len(list(songsList))
        # self.setRowCount(row)
        self.songsList = list(songs)
        self.row = len(self.songsList)
        # self.setColumnWidth(0, 10)

        self.setRowCount(self.row)
        for k,v in enumerate(self.songsList):
            if self.type == 'all':
                self.setItem(k, 0, QTableWidgetItem('  ' + str(self.songsList[k][3])))
                self.setItem(k, 1, QTableWidgetItem(self.songsList[k][1]))
                # self.setItem(k, 2, QTableWidgetItem(''))
                self.setItem(k, 2, QTableWidgetItem(self.songsList[k][4]))
                self.setItem(k, 3, QTableWidgetItem(self.songsList[k][2]))
                self.setItem(k, 4, QTableWidgetItem(self.songsList[k][6] + '  '))
                # self.item(k, 3).setTextAlignment(Qt.AlignCenter)
                self.item(k, 4).setTextAlignment(Qt.AlignRight | Qt.AlignCenter)
            elif self.type == 'album':
                self.setItem(k, 0, QTableWidgetItem('  ' + str(self.songsList[k][3])))
                self.setItem(k, 1, QTableWidgetItem(self.songsList[k][1]))
                # self.setItem(k, 2, QTableWidgetItem(''))
                self.setItem(k, 2, QTableWidgetItem(self.songsList[k][4]))
                self.setItem(k, 3, QTableWidgetItem(self.songsList[k][6]  + '  '))
                self.item(k, 3).setTextAlignment(Qt.AlignRight | Qt.AlignCenter)
            # self.item(k,2).setTextAlignment(Qt.AlignCenter)
            # self.item(k, 2).setForeground(QBrush(QColor(34, 92, 127))) # 设置字体颜色

        self.getPlayingSong()

    # def mouseDoubleClickEvent(self, QMouseEvent):
    #     #     print(11212)
    def playSong(self,Item=None):
        row = Item.row()
        title = self.item(row, 1).text()
        artist = self.item(row, 3).text()
        print(self.songsList[row])
        CommonSignal.playChooseSongSignal.emit(self.songsList, row)

        # data = [str(row), str(1), text]
        # print(Item.row())
    def getPlayingSong(self,songId = None):
        # info = LocalMusic.getPlayingInfo(self)
        # songId = info[0]
        if songId:
            pass
        else:
            info = LocalMusic.getPlayingInfo(self)
            if info:
                songId = info[0]
        for k,v in enumerate(self.songsList):
            if v[0] == songId:
                for i in range(0, self.column):
                    self.item(k, i).setForeground(QBrush(QColor(34, 92, 127))) # 设置字体颜色

                    status_item = QtWidgets.QTableWidgetItem()
                    status_icon = QtGui.QIcon()
                    status_icon.addPixmap(QtGui.QPixmap(":/icons/images/icon/play_5.png"), QtGui.QIcon.Normal,
                                          QtGui.QIcon.Off)
                    status_item.setIcon(status_icon)
                    self.setItem(k, 0, status_item)
                    self.setIconSize(QtCore.QSize(24, 24))
                    color = self.item(k, 1).background()
                    self.setRowColor(k, color)


            else:
                for i in range(0, self.column):
                    self.item(k, i).setForeground(QBrush(QColor(0, 0, 0))) # 设置字体颜色
                    self.setItem(k, 0, QTableWidgetItem('  ' + str(self.songsList[k][3])))



